package com.totobox.apps;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import com.totobox.apps.activity.BasicActivity;
import com.totobox.apps.activity.MyTotoActivity;
import com.totobox.apps.activity.TotoLoginActivity;
import com.totobox.apps.common.FileUtils;
import com.totobox.apps.common.MoveActivity;
import com.totobox.apps.common.TotoIFData;

import com.totobox.apps.Const;

public class TotoBox extends BasicActivity {
	
	private MoveActivity go;
	private Handler mHandler = null;
	private InitThread thread = null;
	
	private boolean isNext = true;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //sqlite db를 assets에서 옮긴다.
        setDB();
        
        DisplayMetrics matrix = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(matrix);
        
        float density = matrix.density;
        float xdpi = matrix.xdpi;
        float ydpi = matrix.ydpi;
        
        
        //super.showText(this, "density >>>>> "+density, Toast.LENGTH_LONG);
        
        Log.i("density ", ">>>>> "+density);
        Log.i("xdpi ", ">>>>> "+xdpi);
        Log.i("ydpi ", ">>>>> "+ydpi);
        
        //초기 폰의 해상도 체크
        Const.WIDTH = matrix.widthPixels;
        
        //시스템 정검을 체크 한다.
        isNext = TotoIFData.SystemCheck();
        
        if(isNext){
        	
        	FileUtils.setMakeDir("/sdcard/totofun/");
        	
        	this.appStart(2000);
        	
        }	
        else
        	super.showText(this, getResources().getString(R.string.system_info), Toast.LENGTH_LONG);
        
    }
    
    public void appStart(int delay){
    	thread = new InitThread();
        mHandler = new Handler();
        mHandler.postDelayed(thread, delay);
    }
    
    class InitThread implements Runnable{
    	
		public void run() {
			
			// TODO Auto-generated method stub
			
			SharedPreferences pref = TotoBox.this.getSharedPreferences(Const.APP_NAME, MODE_PRIVATE);
			
			boolean isAuto = pref.getBoolean("totobox_auto", false);
			String login_id = pref.getString("totobox_id", "");
			
			if(isAuto){
				TotoIFData.jsonLogin(login_id , pref.getString("totobox_pw", "") , "android");
				go = new MoveActivity(TotoBox.this, MyTotoActivity.class);
				//intent = new Intent(TotoBox.this, MyTotoActivity.class);
				//intent.putExtra("login_id", login_id);
			}
			else{
				go = new MoveActivity(TotoBox.this, MyTotoActivity.class);
				//intent = new Intent(TotoBox.this, TotoLoginActivity.class);
				go.putExtra("login_id", login_id);
			}
			go.startActivity(TotoBox.this);
		}
	}
    
    public void setDB() {
		  File folder = new File(Const.DB_DIR);
		  if(folder.exists()) {
			 
		  }
		  else {
			  folder.mkdirs();
		  }

		  AssetManager assetManager = getResources().getAssets();
		  File outfile = new File(Const.DB_DIR+"totofun.db"); //--폰에 위치할 경로
		  
		  InputStream is = null; 
		     
		  FileOutputStream fo = null;
		   
		  long filesize = 0;
		         
		  		     
		  try {
			  // --asset 폴더 및 복사할 DB 지정
			  is = assetManager.open("totofun.db", AssetManager.ACCESS_BUFFER);  
			  filesize = is.available(); //--사이즈 검증
		     
			  // 파일이 없거나 패키지 폴더에 설치된 DB파일이 포함된 DB파일 보다 크기가 같지않을 경우 DB파일을 덮어 쓴다.
			  if (outfile.length() <= 0) {
				  byte[] tempdata = new byte[(int) filesize];
				  is.read(tempdata); 
				  is.close(); 
				  outfile.createNewFile();
				  fo = new FileOutputStream(outfile);
				  fo.write(tempdata);
				  fo.close();
			  }
			  else
			  {
//				  Toast.makeText(this, "db있음", Toast.LENGTH_LONG).show();
			  }
		  } catch (IOException e) { 
//			  Toast.makeText(this, "db이동실패", Toast.LENGTH_LONG).show();
		  }   
	}
}